<?

/**
* Use $_SESSION["GDate"] if you are using custom date
* or just use the system date 
*
* $today_date = $_SESSION["GDate"];
* $today_date = date("d-m-Y");
*
*/
$today_date		  = date("d-m-Y");

$thisSequence   = "PO_SEQ";

$mnTag          = procMenuTag($_SESSION["source_caller"]);

$srcDbSequence      = "MST_SEQUENCE";
$srcDbMstCategory   = "MST_MODEL_CAT";
$srcDbMstGroup      = "MST_MODEL_GRP";
$srcDbHdr			      = "PCH_ORDER_HDR";
$srcDbHdrLog	      = "LOG_PCH_ORDER_HDR";
$srcDbDtl           = "PCH_ORDER_DTL";
$srcDbSubDtl        = "PCH_ORDER_SUB_DTL";

$UID     = unserialize($_SESSION["serUID"]);

$userId  = $UID->getUID();

$COracle = new oracleDb;
$COracle->init($_SESSION["ORCL"], 
               $_SESSION["OPASS"],
               $_SESSION["SID"],
               _NO_COMMENT);

if($COracle->connect()){
	
	$thisPeriode = &$_SESSION["sysPeriode"];
	
	/**
	* Fetch the sequence number
	* and below is the how to
	* 
	* 1. we fetch the current number
	* 2. we update the current number
	* 3. if the fetched number as condition is  
	*    valid then there is no interuption while
	*    updating
	*
	*/
	
	$seqEditStatus = false;
	
	while($seqEditStatus == false){
	  
  	fetchCurSequence($COracle,
    	               $thisSequence,
    	               $thisPeriode,
    	               $srcDbSequence,
    	               $srcJidIndex);
  	
  	initCurSequence($COracle,
    	              $thisSequence,
    	              $srcJidIndex,
    	              $thisPeriode,
    	              $srcDbSequence,
    	              $seqEditStatus);
  }
  
  $COracle->commit();
  
  /**
  * Extract the system period
  *
  */
  $tmpDate = explode("-", $thisPeriode);
  $wrkDate = $tmpDate[0]. /* This period month */
             "-".
             $tmpDate[1]; /* This period year */
  
  $jobId  = addDigit((string) $srcJidIndex). 
            $srcJidIndex. 
            "/" . 
            $wrkDate;
  
  /**
  * Insert all data
  *
  */
  for($iRow = 0; $iRow < $dataup; $iRow++){
    
    fetchIdCategory($COracle,
                    $_POST["categoryEnt"][$iRow],
                    $srcDbMstCategory,
                    $idCategory);
    
    fetchIdGroup($COracle,
                 $_POST["groupEnt"][$iRow],
                 $srcDbMstGroup,
                 $idGroup);
    
    $values = " '".$jobId."', ".                      /* Job id */
              "  ".$idCategory." , ".                 /* Cat id */
              "  ".$idGroup." , ".                    /* grp id */
              " '".$_POST["modelEnt"][$iRow]."', ".   /* Model name */
              "  ".$_POST["modelIdEnt"][$iRow]." , ". /* Model Id */
              " '".$_POST["unitEnt"][$iRow]."', ".    /* Model unit */
              "  ".$_POST["qtyEnt"][$iRow]." , ".     /* Model qty */
              " '".$_POST["cvtEnt"][$iRow]."', ".     /* Model converted to */
              "  ".$_POST["cvtQtyEnt"][$iRow]." , ".  /* Model converted qty */
              "  0 , ".                               /* Receive Qty, it's 0 default */
              "  ".$_POST["priceEnt"][$iRow]." , ".   /* Model price */
              "  ".$_POST["hrzTotEnt"][$iRow].",  ".  /* horizontal total */
              "  ".($iRow + 1)."  ";                  /* no, incr-ed by 1 since $iRow starts from 0 */
    
    insToDbSubDtlPchOrder($COracle,
                          $values,
                          $srcDbSubDtl); 
  }
  
  /**
  * Insert header and master details
  *
  */
  $values = " '".$jobId."', ".                  /* JOB id */
            "  ".$_POST["totQtyEnt"]." , ".     /* total qty */
            "  ".$_POST["totCvtQtyEnt"]." , ".  /* total converted qty */
            "  ".$_POST["grandTotEnt"]." ";     /* Grand total */
  
  insToDbDtlPchOrder($COracle,
                     $values,
                     $srcDbDtl);
  
  $hdrDate = "TO_DATE('".$_POST["trDateEnt"]."', 'dd-mm-YYYY')";
                     
  fetchHdrPchOdrNo($COracle,
                   $_POST["trDateEnt"],
                   $srcDbHdr,
                   $hdrNo);
  
  $values = " '".$jobId."', ".                /* Job id */
            " ' ',    ".                      /* Reference id ' ' default */
            "  ".$hdrDate." , ".              /* Tx date */
            " '".$_POST["supplierEnt"]."', ". /* supplier name */
            " '".$_POST["currencyEnt"]."', ". /* currency */
            "  ".$_POST["rateEnt"]." , ".     /* Rate */
            " '".$_POST["memoEnt"]."', ".     /* Memo */
            "  ".$hdrNo.", ".                 /* Number */
            " '"._NOT_ACTIVE."' ";            /* Status */
  
  
  insToDbHdrPchOrder($COracle,
                     $values,
                     $srcDbHdr);            
            
  /**
  * Insert Log
  *
  */
  insToDbLog($COracle,
             $today_date,
             $jobId,
             $userId,
             $_SERVER["REMOTE_ADDR"],
             _ENTRY_MODE,
             $srcDbHdrLog);
                                
	$COracle->commit();
	
	if($COracle->getStatus()){
	
	  usrMsg("Upload Complete");
	}else{
	  
	  usrMsg("Upload Process Interupted");
	}
	
	$COracle->dc();
	
	$restore  = 0;
	$uprun    = 0;
	$dataup   = 0;
	
	//jumpTo(2,"../".$_SESSION["source_caller"]);
	
}else{
	echo "<br/><font size='16'>Error reason: upload connection failed </font>\n";
}

?>